/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.views;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import pe.edu.pucp.Servicio.Servicio_Usuario;
import pe.edu.pucp.bean.Usuario;
import javax.swing.JOptionPane;

/**
 *
 * @author LiziTa
 */
public class PanelUsuarioEliminar extends javax.swing.JPanel {

       Servicio_Usuario service = Servicio_Usuario.get_instance();
    private String idUser;
   TablaModel tm=new TablaModel();
    String cad=null;
    
    
    public String getIdUser(){
        return idUser;
    }
    public void setIdUser(String id){
        this.idUser=id;
    }
    
    class  TablaModel extends AbstractTableModel{
       
        ArrayList<Usuario> users =service.queryAllUsuarios();
        String Titulos[]={"ID","NOMBRE","APELLIDOP","IP","CLAVE"};
        
        @Override
        public int getRowCount() {
            return users.size();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        @Override
        public Object getValueAt(int fil, int col) {
            String res=null;
            Usuario c = users.get(fil);
            
            switch (col){
                case 0 : res =""+c.getId();break;
                case 1 : res= c.getNombre();break;
                case 2 : res= c.getApellidoP();break;
                //case 3 : res= c.getApellidoM();break;
                case 3 : res= c.getIp();break;
                case 4 : res= c.getContrasena();break;

            }
            return res;
            
        }
        //PARA PODER EDITAR UNA TABLA
//        @Override
//        public boolean isCellEditable(int fil,int col){
//            if (col==3) 
//                return true;
//            else 
//                return false;
//        }
//        @Override
//        public void setValueAt(Object dato,int fil,int col){
//        if (col==3){   
//            Usuario u=users.get(fil);
//            txtIp.setText(Integer.toString(u.getIp()));            
//            fireTableCellUpdated(fil,col);
//            }    
//        }
        
        @Override
        public String getColumnName(int col){
            return Titulos[col];
        }
    }
    public void refreshDetails(){
     
    }

    public void Actualizar(){
        tm.users=service.queryAllUsuarios();
        tm.fireTableChanged(null);
    }
    public void ActualizarBusqueda(ArrayList<Usuario> u){
        tm.users=u;
        tm.fireTableChanged(null);
    }
    public PanelUsuarioEliminar() {
        initComponents();
        TablaUsuario.setModel(tm);        
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaUsuario = new javax.swing.JTable();
        Eliminar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Usuario"));

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/pucp/views/logo.gif"))); // NOI18N

        jLabel1.setText("Tabla de Usuarios Tramarsa");

        TablaUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaUsuarioMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaUsuario);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(132, 132, 132)
                        .addComponent(jLabel13))
                    .addComponent(jScrollPane1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1))
                    .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        Eliminar.setText("Eliminar");
        Eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarActionPerformed(evt);
            }
        });

        Cancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(145, 145, 145)
                        .addComponent(Eliminar)
                        .addGap(55, 55, 55)
                        .addComponent(Cancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Eliminar)
                    .addComponent(Cancelar))
                .addContainerGap(18, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void EliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarActionPerformed
        
 int rowSel=TablaUsuario.getSelectedRow();
        String id=TablaUsuario.getValueAt(rowSel,0).toString();     
         int p= JOptionPane.showConfirmDialog(null,"Desea eliminar la fila seleccionada","Eliminar",JOptionPane.YES_NO_OPTION);
         if (p==0){
            service.eliminaUsuario(id);
            Actualizar();   
         }
         
         
    }//GEN-LAST:event_EliminarActionPerformed

    private void TablaUsuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaUsuarioMouseClicked
       int rowSel = TablaUsuario.getSelectedRow();
        String idUsuario=TablaUsuario.getValueAt(rowSel,0).toString();
        Usuario user=service.queryByUsuario(idUsuario);
 
       
    }//GEN-LAST:event_TablaUsuarioMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancelar;
    private javax.swing.JButton Eliminar;
    private javax.swing.JTable TablaUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
